﻿using Emgu.CV;
using Emgu.CV.Structure;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 仪表识别2
{
    class Magnify
    {
        public PictureBox srcBox { get; set; }
        private float multiple;
        private PictureBox resBox;

        public Magnify(PictureBox srcBox, float multiple)
        {
            this.srcBox = srcBox;
            this.multiple = multiple;
        }
        public Magnify(PictureBox srcBox, float multiple, PictureBox resBox)
        {
            this.srcBox = srcBox;
            this.multiple = multiple;
            this.resBox = resBox;
        }
       
        public void enlarge(int x, int y)
        {
            if (srcBox.Image != null)
            {
                int res_w = 50;
                int res_h = 50;
                if (resBox == null)
                {
                    PictureBox rb = new PictureBox();
                    rb.SizeMode = PictureBoxSizeMode.Zoom;
                    rb.Size = new Size(100, 100);
                    rb.BringToFront();
                    rb.Parent = srcBox;
                    resBox = rb;
                }
                resBox.Top = y - 120;
                resBox.Left = x - 120;

                try
                {
                    Bitmap partImg = new Bitmap(res_w, res_h);
                    Graphics graphics = Graphics.FromImage(partImg);
                    Rectangle destRect = new Rectangle(0, 0, res_w, res_h);//目标位置
                    int w = srcBox.Image.Width;
                    int h = srcBox.Image.Height;
                    int cw = srcBox.Width;
                    int ch = srcBox.Height;
                    float bl_w = ((float)cw) / ((float)w);
                    float bl_h = ((float)ch) / ((float)h);
                    float bl = Math.Min(bl_w, bl_h);
                    int vw = (int)(w * bl);
                    int vh = (int)(h * bl);
                    int x0 = (cw - vw) / 2;
                    int y0 = (ch - vh) / 2;
                    x = (int)((x - x0) / bl);
                    y = (int)((y - y0) / bl);
                    Rectangle origRect = new Rectangle(x - res_w/2, y - res_h/2, res_w, res_h);
                    graphics.DrawImage(this.srcBox.Image, destRect, origRect, GraphicsUnit.Pixel);
                    Image<Bgr, byte> resImg = new Image<Bgr, byte>(partImg);
                    resImg.Draw(new LineSegment2D(new Point(res_w/2, 0), new Point(res_w/2, res_h)), new Bgr(Color.Red), 1);
                    resImg.Draw(new LineSegment2D(new Point(0, res_w/2), new Point(res_w, res_w/2)), new Bgr(Color.Red), 1);
                    
                    resBox.Image = resImg.Bitmap;
                }
                catch (Exception e)
                {
                    Log.error(e.Message);
                }
            }
        }

        //private Form getMaxTopForm(Control control)
        //{
        //    if(control is Form)
        //    {
        //        return (Form)control;
        //    }
           
        //    return  getMaxTopForm(control.Parent);
        //}
    }
}
